<?php

class MenutypeController extends AdminController {
    const MODEL_CLASS_NAME = "MenuType";
    
    public function actionIndex() {
        $model = new MenuType('search');
        $model->unsetAttributes();  // clear any default values
	
        if (isset($_GET[self::MODEL_CLASS_NAME])) {
            $model->attributes = $_GET[self::MODEL_CLASS_NAME];
	    Yii::app()->user->setState(app()->controller->id . self::MODEL_CLASS_NAME,$_GET[self::MODEL_CLASS_NAME]);
        } else if(isset($_POST[self::MODEL_CLASS_NAME])) {
	    $model->attributes = $_POST[self::MODEL_CLASS_NAME];
	    Yii::app()->user->setState(app()->controller->id . self::MODEL_CLASS_NAME,$_POST[self::MODEL_CLASS_NAME]);
	} else {
            $modelState = Yii::app()->user->getState(app()->controller->id . self::MODEL_CLASS_NAME) ;
            if($modelState != null)
                    $model->attributes = $modelState;    
        }
	
        $this->render('index', array('model' => $model,));
    }

    public function actionRefresh(){
        Yii::app()->user->setState(app()->controller->id . self::MODEL_CLASS_NAME, null);
        $this->redirect(url(app()->controller->id));
    }
    
    function actions() {
        return array(
            'create' => array(
                'class' => 'lib.components.actions.CreateAction',
                'modelClass' => self::MODEL_CLASS_NAME,
                'messages' => array('success' => t('MSG_INFO_INSERT_SUCCESS', 'messages', array('{0}' => t('LBL_MENU_TYPE', 'menuType'))))
            ),
            'update' => array(
                'class' => 'lib.components.actions.UpdateAction',
                'modelClass' => self::MODEL_CLASS_NAME,
                'messages' => array('success' => t('MSG_INFO_UPDATE_SUCCESS', 'messages', array('{0}' => t('LBL_MENU_TYPE', 'menuType'))))
            ),
            'delete' => array(
                'class' => 'lib.components.actions.DeleteAction',
                'ajaxView' => "//share/_delete",
		'validateMethod' => 'validateDelete',
                'messages' => array('success' => t('MSG_INFO_DELETE_SUCCESS', 'messages', array('{0}' => t('LBL_MENU_TYPE', 'menuType'))))
            ),
        );
    }

    public function loadModel() {
        $model = null;
        if (isset($_GET['id'])) {
            $ids = explode(',', $_GET['id']);
            if (isset($ids)) {
                $criteria = new CDbCriteria;
                $criteria->addInCondition('id', $ids);
                $model = MenuType::model()->findAll($criteria);
            }
        }
        return $model;
    }

    
    public function validateDelete($models) {
     $msg = null;
        foreach ($models as $menuType) {
            if (!$menuType->checkCanDelete()) {
                $msg = new LGMessage(MsgType::ERROR, t('MSG_ERROR_DELETE_UNSUCCESS', 'messages'));
                return $msg;
            } 
        }
        return $msg;
    }
    
// TODO: Comment by Kien.Tran
//    public function actionActive($id) {
//        if (Yii::app()->request->isAjaxRequest) {
//            if ($id != '') {
//                $msgStatus = 'actived';
//                $menuType = MenuType::model()->findByPk($id);                
//                if($menuType->status == LGConstant::PUBLISH_STATUS)
//                {
//                    $menuType->status = LGConstant::UNPUBLISH_STATUS;
//                    $msgStatus = 'inactived';
//                }
//                else
//                {
//                    $menuType->status = LGConstant::PUBLISH_STATUS;
//                }
//                $menuType->saveAttributes(array('status'));
//                echo CJSON::encode( array(
//                    'status' => MsgType::SUCCESS,
//                    'content' => Yii::t('messages', 'MSG_INFO_SUCCESS', array('{0}' => t('LBL_MENU_TYPE', 'menuType'),'{1}'=>$msgStatus))
//                    ));
//                app()->end();
//            }
//        }
//    }


}

?>